import { Request, Response, NextFunction } from 'express'
import { getPlayerStatistics, getOverviewStatistics } from '../services/statisticsService'
import { ApiResponse } from '../types/api'
import { sendError, AppError } from '../utils/errors'

// 获取选手统计信息
export const getPlayerStatisticsHandler = async (_req: Request, res: Response, next: NextFunction) => {
  try {
    const statistics = await getPlayerStatistics()

    const response: ApiResponse = {
      success: true,
      data: statistics,
      message: '统计信息获取成功'
    }

    res.json(response)
  } catch (error) {
    if (error instanceof AppError) {
      sendError(res, error)
    } else {
      next(error)
    }
  }
}

// 获取概览统计
export const getOverviewStatisticsHandler = async (_req: Request, res: Response, next: NextFunction) => {
  try {
    const statistics = await getOverviewStatistics()

    const response: ApiResponse = {
      success: true,
      data: statistics,
      message: '概览统计获取成功'
    }

    res.json(response)
  } catch (error) {
    if (error instanceof AppError) {
      sendError(res, error)
    } else {
      next(error)
    }
  }
}
